package com.yuanluludev.app.navigationview

import android.os.Bundle
import androidx.fragment.app.Fragment
import com.yuanluludev.app.R
import com.yuanluludev.app.bean.Person
import com.yuanluludev.lib_base.base.BaseVMTwiceExitActivity
import com.yuanluludev.lib_base.base.BaseViewModel
import com.yuanluludev.pgy.Pgy
import kotlinx.android.synthetic.main.activity_navigation.*

/**
 * 导航 Demo
 */
class NavigationActivity : BaseVMTwiceExitActivity<BaseViewModel>() {

    private lateinit var fragments: Map<Int, Fragment>

    private lateinit var p: Person

    override fun getLayoutResId(): Int {
        return R.layout.activity_navigation
    }

    override fun initEvents(savedInstanceState: Bundle?) {

        fragments = mapOf(
            R.id.home to createFragment(BlankOneFragment::class.java),
            R.id.system to createFragment(BlankTwoFragment::class.java),
            R.id.discovery to createFragment(BlankThreeFragment::class.java),
        )

        bottomNavigationView?.run {
            setOnNavigationItemSelectedListener { menuItem ->
                showFragment(menuItem.itemId)
                true
            }
            setOnNavigationItemReselectedListener { menuItem ->
            }
        }

        if (savedInstanceState == null) {
            val initialItemId = R.id.home
            bottomNavigationView?.selectedItemId = initialItemId
            showFragment(initialItemId)
        }

//        showOrHideBadgeView(R.id.home,999)

        Pgy.checkVersionUpdate(this, false)
    }

    private fun createFragment(clazz: Class<out Fragment>): Fragment {
        var fragment = supportFragmentManager.fragments.find { it.javaClass == clazz }
        if (fragment == null) {
            fragment = when (clazz) {
                BlankOneFragment::class.java -> BlankOneFragment()
                BlankTwoFragment::class.java -> BlankTwoFragment()
                BlankThreeFragment::class.java -> BlankThreeFragment()
                else -> throw IllegalArgumentException("argument ${clazz.simpleName} is illegal")
            }
        }
        return fragment
    }

    private fun showFragment(menuItemId: Int) {
        val currentFragment = supportFragmentManager.fragments.find {
            it.isVisible && it in fragments.values
        }
        val targetFragment = fragments[menuItemId]
        supportFragmentManager.beginTransaction().apply {
            currentFragment?.let { if (it.isVisible) hide(it) }
            targetFragment?.let {
                if (it.isAdded) show(it) else add(R.id.flContainer, it)
            }
        }.commit()
    }

    override fun viewModelClass() = BaseViewModel::class.java


    /**
     * 显示或者隐藏底部 badge view 并且显示消息数字
     */
    private fun showOrHideBadgeView(menuItemId: Int, num: Int) {
        bottomNavigationView?.apply {
            val badge = getOrCreateBadge(menuItemId)
            if (num <= 0) {
                badge.isVisible = false
            } else {
                badge.isVisible = true
                badge.number = num
            }
        }
    }

}

